package com.sxt.action;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sxt.entity.Student;

@SuppressWarnings("serial")
public class StudentAction extends ActionSupport {
	private Student student;
	private static Map<Integer, Student> students = new HashMap<Integer, Student>();
	private int id;

	/**
	 * 添加学生
	 * 
	 * @return 处理结果
	 */
	public String add() {
		if (null == student) {
			return Action.INPUT;
		}

		Map<String, Object> session = ActionContext.getContext().getSession();

		System.out.println(student);
		System.out.println(students);

		students.put(this.student.getId(), this.student);
		session.put("students", students);
		
		System.out.println("student"+student);

		return Action.SUCCESS;
	}

	/**
	 * 更新学生信息
	 * 
	 * @return 处理结果
	 */
	public String update() {
		if (id == 0) {
			return Action.ERROR;
		}
		Map<String, Object> session = ActionContext.getContext().getSession();
		@SuppressWarnings("unchecked")
		Map<Integer, Student> sts = (Map<Integer, Student>) session.get("students");
		student = sts.get(id);
		
		return Action.INPUT;
	}
	
	/**
	 * 删除学生信息
	 * 
	 * @return 处理结果
	 */
	public String remove() {
		if (id == 0) {
			return Action.ERROR;
		}
		
		return Action.SUCCESS;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
